package edu.odu.cs.sstore.client;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import edu.odu.cs.sstore.dto.UserDTO;
import edu.odu.cs.sstore.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/user",method=RequestMethod.GET)
	public ModelAndView get() {
		return new ModelAndView("users","users",this.userService.getUsers());
	}
	
	@RequestMapping(value="/user/add",method=RequestMethod.GET)
	public ModelAndView addUser() {
		ModelAndView mav = new ModelAndView("user");
		mav.addObject("operation", "Add");
		mav.addObject("user",new UserDTO());
		return mav;
	}
	
	@RequestMapping(value="/user/add", method=RequestMethod.POST)
	public Object $addUser(
			@ModelAttribute("user") @Valid UserDTO user,
			BindingResult result, 
			HttpServletRequest request, 
			HttpServletResponse response) 
	{
		// If there are errors, return to the view
		if(result.hasErrors()) {
			ModelAndView mav = new ModelAndView("user");
			mav.addObject("operation","Add");
			mav.addObject("user",user);
			return mav;
		}
		
		// Create the user
		this.userService.createUser(user);
		// Redirect back to the user list
		return new RedirectView("/user",true);
	}
	
	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	public ModelAndView editUser(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView("user");
		mav.addObject("operation", "Edit");
		mav.addObject("user",this.userService.getUser(id));
		return mav;
	}
	
	@RequestMapping(value="/user/{id}",method=RequestMethod.POST)
	public ModelAndView $editUser(
			@ModelAttribute("user") @Valid UserDTO user,
			BindingResult result, 
			HttpServletRequest request, 
			HttpServletResponse response) 
	{
		// Validate password
		if(!result.getFieldValue("password").equals(request.getParameter("confirmPassword"))) {
			result.rejectValue("password",null,"Passwords must match.");
		}
		
		// If there are errors, return to the view
		if(result.hasErrors()) {
			ModelAndView mav = new ModelAndView("user");
			mav.addObject("operation", "Edit");
			mav.addObject("user",user);
			return mav;
		}
		
		// Update the user
		user = this.userService.updateUser(user);
		
		// Return the new information
		ModelAndView mav = new ModelAndView("user");
		mav.addObject("operation", "Edit");
		mav.addObject("user",user);
		return mav;
		
	}
}
